<template>
    <div class="login-container">
        <div class="content">
            <img src="@/assets/images/kwt.png" alt="" class="logo">
            <h3>康卫通首页智能质控小助手</h3>
            <el-form class="form" ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
                <el-form-item label="账号" required prop="username">
                    <el-input v-model="form.username" @keyup.enter.native="onSubmit"></el-input>
                </el-form-item>
                <el-form-item label="密码" required prop="password">
                    <el-input v-model="form.password" @keyup.enter.native="onSubmit" type="password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button :loading="loading" type="primary" class="btn" @click.native.prevent="onSubmit">登录</el-button>
                </el-form-item>
            </el-form>
            <p class="copyright">
                <img src="@/assets/images/logo.png" alt="">
                <span>康卫通科技成都有限公司版权所有</span>
            </p>
        </div>
    </div>
</template>

<script>
    import {getSystemSet} from '@/api/global'
    import Cookie from 'js-cookie'

    export default {
        name: 'index',
        data () {
            return {
                // 登录表单
                form: {
                    username: '',
                    password: ''
                },
                // 登录加载
                loading: false,
                // 表单验证
                rules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                }
            }
        },
        created () {
        },
        mounted () {
        },
        methods: {
            /**
             * 提交表单
             */
            onSubmit () {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.loading = true
                        this.$store.dispatch('user/loginAuth', this.form)
                        this.$store.dispatch('user/login', this.form).then((res) => {
                            this.loading = false
                            if (res[0] && res[0].code.toString() === '200') {
                                getSystemSet(this.getLocalStorage('userMechanism', 'id')).then(res => {
                                    let value = res.data.filter(item => item.name === '客户端使用模式')[0].value
                                    localStorage.setItem('clientModel', value)
                                    if (value === '2' || value === '0') {
                                        // 没有编辑框
                                        /* eslint-disable */
                                        native.onLoginSuccessed({
                                            assistantUrl: `${window.location.origin}/client/helper`,
                                            token: Cookie.get('token'),
                                            clientMode: value
                                        })
                                        /* eslint-enable */
                                    } else {
                                        /* eslint-disable */
                                        native.onLoginSuccessed({
                                            editorUrl: `${window.location.origin}/client/list`,
                                            assistantUrl: `${window.location.origin}/client/helper`,
                                            token: Cookie.get('token'),
                                            clientMode: value
                                        })
                                        /* eslint-enable */
                                    }
                                })
                            } else {
                                this.$message.error(res.message)
                                this.$store.dispatch('user/logout').then(() => {
                                    // 验证失败，请重新登录
                                    this.$router.push({path: '/login'})
                                })
                            }
                        }).catch(() => {
                            this.loading = false
                        })
                    } else {
                        return false
                    }
                })
            }
        }
    }
</script>

<style scoped lang="scss">
    .login-container {
        .content {
            padding: 10px 20px 20px;
            box-sizing: content-box;
            width: 280px;
            margin: 0 auto;
            .logo {
                display: block;
                width: 30px;
                height: 30px;
                margin: 0 auto;
            }
            h3 {
                font-size: 18px;
                line-height: 36px;
                font-weight: 600;
                text-align: center;
            }
            .btn {
                display: block;
                width: 100%;
                margin-top: 20px;
            }
            .copyright {
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 12px;
                line-height: 24px;
                img {
                    display: block;
                    height: 24px;
                    margin-right: 4px;
                }
            }
        }
    }
</style>
<style lang="scss">
    .form {
        .el-form-item {
            margin-bottom: 8px!important;
        }
        .el-form-item__label {
            padding-bottom: 0!important;
            float: left;
        }
    }
</style>
